package com.nttuan.model.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

import com.nttuan.util.Support;
import com.nttuan.model.DataBaseHelper;
import com.nttuan.model.dao.service.UserService;
import com.nttuan.model.entity.User;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by NT Tuan on 10/21/2014.
 */
public class UserDao implements UserService {

    private DataBaseHelper db;
    private static UserDao userDao;

    public static UserDao getInstance(Context context){
        if(userDao == null)
            userDao = new UserDao(context);
        return userDao;
    }
    public UserDao(Context context){
        try {
            db = new DataBaseHelper(context);
        }catch (Exception e){}
    }

    @Override
    public List<User> users(boolean isActive) {
        db.openDataBase();
        List<User> result = new ArrayList<User>();
        String selection = User.IS_ACTIVE +"=?";
        String[] where = {isActive ? "1":"0"};
        Cursor cursor = db.select(true, User.TABLE_NAME, null, selection, where, null);
        if(cursor != null)
            while (cursor.moveToNext())
                result.add(getItem(cursor));
        db.close();
        return  result;
    }

    @Override
    public List<User> users() {
        db.openDataBase();
        List<User> result = new ArrayList<User>();
        Cursor cursor = db.select(true, User.TABLE_NAME, null, null, null, null);
        if(cursor != null)
            while (cursor.moveToNext())
                result.add(getItem(cursor));
        db.close();
        return  result;
    }

    @Override
    public User user(int userId, boolean isActive) {
        db.openDataBase();
        String selection = User.USER_ID +"=? and " + User.IS_ACTIVE +"=? ";
        String[] where = {userId+"",isActive ? "1":"0"};
        Cursor cursor = db.select(true,User.TABLE_NAME,null,selection,where,null);
        if(cursor != null)
            if (cursor.moveToNext()){
                db.close();
                return getItem(cursor);
            }
        return  null;
    }

    @Override
    public User user(String email) {
        db.openDataBase();
        String selection = User.EMAIL +"=?";
        String[] where = {email};
        Cursor cursor = db.select(true,User.TABLE_NAME,null,selection,where,null);
        if(cursor != null)
            if (cursor.moveToNext()){
                db.close();
                return getItem(cursor);
            }
        return  null;
    }

    @Override
    public User user(int userId) {
        db.openDataBase();
        String selection = User.USER_ID +"=?";
        String[] where = {userId+""};
        Cursor cursor = db.select(true,User.TABLE_NAME,null,selection,where,null);
        if(cursor != null)
            if (cursor.moveToNext()){
                db.close();
                return getItem(cursor);
            }
        return  null;
    }

    @Override
    public User crateOrUpdate(User user) {
        int newId = isExit(user.getEmail());
        if(newId == 0) {
            create(user);
            newId = newId();
        }
        return user(newId);
    }

    @Override
    public int isExit(String email) {
        db.openDataBase();
        String selection = User.EMAIL +"=?";
        String[] where = {email};
        Cursor cursor = db.select(true,User.TABLE_NAME,null,selection,where,null);
        if(cursor != null)
            if (cursor.moveToNext()){
                db.close();
                return cursor.getInt(cursor.getColumnIndex(User.USER_ID));
            }
        return  0;
    }

    @Override
    public boolean checkAndCreate(User user) {
        if(isExit(user.getEmail()) == 0)
            create(user);
        return true;
    }

    @Override
    public User getMe() {
        db.openDataBase();
        List<User> result = new ArrayList<User>();
        Cursor cursor = db.select(true, User.TABLE_NAME, null, null, null, null);
        if(cursor != null)
            while (cursor.moveToNext())
                return getItem(cursor);
        db.close();
       return null;
    }

    @Override
    public int newId(){
        db.openDataBase();
        String[] column ={"max("+User.USER_ID+") as newId"};
        Cursor cursor = db.select(true,User.TABLE_NAME,column,null,null,null);
        if(cursor != null)
            if (cursor.moveToNext()){
                db.close();
                return cursor.getInt(cursor.getColumnIndex("newId"));
            }
        return  1;
    }
    @Override
    public boolean create(User user) {
        db.openDataBase();
        ContentValues values = new ContentValues();
        values.put(User.USER_NAME,user.getUserName());
        values.put(User.EMAIL,user.getEmail());
        values.put(User.PASSWORD,user.getPassword());
        values.put(User.ADDRESS,user.getAddress());
        values.put(User.PHONE,user.getPhone());
        values.put(User.FACEBOOK,user.getFacebook());
        values.put(User.SKYPE,user.getSkype());
        values.put(User.IS_ACTIVE,user.isActive());
        boolean result = db.insert(User.TABLE_NAME,values);
        db.close();
        return result;
    }

    @Override
    public boolean update(User user) {
        db.openDataBase();
        ContentValues values = new ContentValues();
        values.put(User.USER_NAME,user.getUserName());
        values.put(User.EMAIL,user.getEmail());
        values.put(User.PASSWORD,user.getPassword());
        values.put(User.ADDRESS,user.getAddress());
        values.put(User.PHONE,user.getPhone());
        values.put(User.FACEBOOK,user.getFacebook());
        values.put(User.SKYPE,user.getSkype());
        values.put(User.IS_ACTIVE,user.isActive());
        String whereClause = User.USER_ID + "=?";
        String[] whereArgs = {user.getUserId()+""};
        boolean result = db.update(User.TABLE_NAME, values, whereClause, whereArgs);
        db.close();
        return result;
    }

    @Override
    public boolean remove(int userId) {
        db.openDataBase();
        String whereClause = User.USER_ID + "=?";
        String[] whereArgs = {userId+""};
        boolean result = db.delete(User.TABLE_NAME,whereClause,whereArgs);
        db.close();
        return result;
    }

    @Override
    public boolean active(int userId, boolean isActive) {
        db.openDataBase();
        ContentValues values = new ContentValues();
        values.put(User.IS_ACTIVE,isActive);
        String whereClause = User.USER_ID + "=?";
        String[] whereArgs = {isActive ? "1":"0"};
        boolean result = db.update(User.TABLE_NAME, values, whereClause, whereArgs);
        db.close();
        return result;
    }

    @Override
    public boolean chanePassword(int userId, String password) {
        db.openDataBase();
        ContentValues values = new ContentValues();
        values.put(User.PASSWORD,password);
        String whereClause = User.USER_ID + "=?";
        String[] whereArgs = {userId+""};
        boolean result = db.update(User.TABLE_NAME, values, whereClause, whereArgs);
        db.close();
        return result;
    }
    private User getItem(Cursor cursor){
        User user = new User();
        user.setUserId(cursor.getInt(cursor.getColumnIndex(User.USER_ID)));
        user.setUserName(cursor.getString(cursor.getColumnIndex(User.USER_NAME)));
        user.setEmail(cursor.getString(cursor.getColumnIndex(User.EMAIL)));
        user.setPassword(cursor.getString(cursor.getColumnIndex(User.PASSWORD)));
        user.setAddress(cursor.getString(cursor.getColumnIndex(User.ADDRESS)));
        user.setPhone(cursor.getString(cursor.getColumnIndex(User.PHONE)));
        user.setFacebook(cursor.getString(cursor.getColumnIndex(User.FACEBOOK)));
        user.setSkype(cursor.getString(cursor.getColumnIndex(User.SKYPE)));
        user.setActive(Support.toActive(cursor.getString(cursor.getColumnIndex(User.IS_ACTIVE))));
        return user;
    }
}
